﻿Public Class MetaApplications_PL
    Inherits PLBase

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal queryName As String)
        MyBase.New(queryName)
    End Sub

    Public Overrides Function Delete() As Boolean
        Return True
    End Function
    Public Overrides Function Validate() As Boolean
        Return True
    End Function

    Public Shared Function ExportMetaApplications(ByVal filename As String, ByVal applicationID As Guid) As Boolean
        Using ms As MetaApplications_PL = New MetaApplications_PL("MetaApplications")
            Try
                ms.MetaDataTable.GetMetaCommand()
                ms.MetaDataTable.MetaCommand.SQLWhere = String.Format("ApplicationID = '{0}'", applicationID)
                ms.InitDataForExport()
                ms.Dataset.DataSetName = applicationID.ToString
                ms.Dataset.WriteXml(filename, XmlWriteMode.WriteSchema)
                Return True
            Catch ex As Exception
                Throw
            End Try
            Return False
        End Using
    End Function

    Public Shared Function ExportMetaApplicationsToMaster(ByVal applicationID As Guid) As Boolean
        Dim IsSaved As Boolean
        Using mcSource As MetaApplications_PL = New MetaApplications_PL("MetaApplications")
            Try
                mcSource.MetaDataTable.GetMetaCommand()
                mcSource.MetaDataTable.MetaCommand.SQLWhere = String.Format("ApplicationID = '{0}'", applicationID)
                mcSource.InitDataForExport()
                mcSource.Dataset.DataSetName = applicationID.ToString
                For Each oTable As DataTable In mcSource.Dataset.Tables
                    For Each oRow As DataRow In oTable.Rows
                        oRow.SetAdded()
                    Next
                Next

                'Bypass pour ne plus utiliser la connection par défaut
                AppManager.IsConnected = False
                AppManager.OverrideByPassConnectionStringTOMaster = True

                'Indique qu'elle connection utiliser dans le App.Config
                AppManager.ConnectionConfigName = "ConnectionToMyMaster"
                Using mcDest As MetaApplications_PL = New MetaApplications_PL("MetaApplications")
                    mcDest.InitData()
                    mcDest.Dataset.Merge(mcSource.Dataset)
                    IsSaved = mcDest.Save()
                End Using
                Return IsSaved
            Catch ex As Exception
                Throw ex
            Finally

                'Remet en route la connection par défaut
                AppManager.IsConnected = True
                AppManager.OverrideByPassConnectionStringTOMaster = False

                'Change pour utiliser la connection par défaut
                AppManager.ConnectionConfigName = Nothing
            End Try
            Return False
        End Using
    End Function

    Public Shared Function ImportMetaApplications(ByVal fileName As String) As Boolean
        Using dset As DataSet = New DataSet()
            dset.ReadXml(fileName, XmlReadMode.ReadSchema)
            Using ms As MetaApplications_PL = New MetaApplications_PL("MetaApplications")
                'Hack: DataSet must be fill to have adaptercommand and merge...
                ms.InitData()
                ms.Dataset.Merge(dset)
                Return ms.Save()
            End Using
        End Using
    End Function

    Public Function InitDataForExport() As Boolean
        Dim metaDataTableChild As CommonExt.MetaDataTable
        Dim aChildRelationField As String
        Dim parentGuid As Guid = Guid.Empty
        Try
            Me.MetaDataTable.FillBusinessApplicationData()
            Me.DataSet.Tables.Add(Me.MetaDataTable)
            'ici on veux un record seulement
            If Not String.IsNullOrEmpty(Me.MetaDataTable.MetaCommand.SQLWhere) Then
                parentGuid = CType(Me.MetaDataTable.Rows(0).Item(0), Guid)
            End If
            Dim aParentRelationField As String = Me.MetaDataTable.MetaCommand.GUIDField
            For Each row As DataRow In Me.MetaDataTable.MetaCommand.GetChildDisplayQuery.Rows
                metaDataTableChild = New CommonExt.MetaDataTable() With {.QueryName = row("QueryName").ToString}
                aChildRelationField = Me.MetaDataTable.MetaCommand.GUIDField & "_FK"
                '
                metaDataTableChild.GetMetaCommand()
                '
                If parentGuid <> Guid.Empty Then
                    metaDataTableChild.MetaCommand.SQLWhere = String.Format("{0} = '{1}'", aChildRelationField, parentGuid)
                End If
                '
                metaDataTableChild.FillBusinessApplicationData()
                '
                Me.DataSet.Tables.Add(metaDataTableChild)
                Dim aDR As New DataRelation(row("Relation").ToString, Me.MetaDataTable.Columns(aParentRelationField), metaDataTableChild.Columns(aChildRelationField))
                Me.DataSet.Relations.Add(aDR)
            Next
            Return True
        Catch ex As Exception
            Throw
        End Try
        Return False
    End Function

End Class
